package com.lesent.model.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.MapType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shengtao
 * @Description:
 * @date 2018/08/15 13:25
 */
public class JsonUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);

    /**
     * 对象转化json
     *
     * @param object
     * @return
     * @throws JsonProcessingException
     */
    public static String obj2Json(Object object) throws JsonProcessingException {
        String json = "";
        if(null == object){
            return json;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper = settings(objectMapper);
        json = objectMapper.writeValueAsString(object);
        return json;
    }

    /**
     * json转化对象
     */
    public static <T> T json2Obj(String json,Class<T> tClass) throws IOException {
        T obj = null;
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper = settings(objectMapper);
        obj =objectMapper.readValue(json,tClass);
        return obj;
    }

    /**
     * json转list
     */
    public static <T> List<T> json2List(String json, Class<T> tClass) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper = settings(objectMapper);
        JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class,tClass);
        List<T> tList = null;
        try {
            tList = objectMapper.readValue(json,javaType);
        } catch (IOException e) {
            LOGGER.error("JsonUtils 解析 json异常：{}",e.getMessage());
        }
        return tList;
    }

    /**
     * json转map
     */
    public static <K,V> Map<K,V> json2Map(String json,Class<K> kClass,Class<V> vClass) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        JavaType key = objectMapper.getTypeFactory().constructType(kClass);
//        JavaType value = objectMapper.getTypeFactory().constructType(vClass);
        MapType type = objectMapper.getTypeFactory().constructMapType(Map.class,kClass,vClass);
//        MapType type = objectMapper.getTypeFactory().constructMapType(Map.class,key,value);
        Map<K,V> map = new HashMap<>();
        map = objectMapper.readValue(json,type);
        return map;
    }

    private static ObjectMapper settings(ObjectMapper objectMapper){
        DateFormat dateFormat = new SimpleDateFormat(DateUtils.DEFAULT_DATETIME_PATTERN);
        objectMapper.setDateFormat(dateFormat);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.getSerializationConfig().with(JsonGenerator.Feature.IGNORE_UNKNOWN);
        return objectMapper;
    }
}
